<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Contact extends CI_Controller {

	public $SiteName = 'Công Ty TNHH MTV Kim Hạnh Việt';
	function __construct()
	{
		//if($this->router->class != "news" and $this->router->method !="search") $this->session->unset_userdata("search");
		parent::__construct();
		$this->load->helper('captcha');
		$this->load->library('form_validation');
		$this->load->library('PHPMailer');
		if (empty($_SESSION['lang'])) {
			$this->lang->load('home', 'english');
			$this->lang->load('form_validation', 'english');
			$this->SiteName = 'Kim Hanh Viet Limited Liability Company';
		} else {
			$this->lang->load('home', 'vietnamese');
			$this->lang->load('form_validation', 'vietnamese');
		}
	}

	function index() {
		$this->session->unset_userdata("search");
		if($this->input->post("send")) {
			if (empty($_SESSION['lang'])) {
				$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
				$this->form_validation->set_rules('name', 'Full Name', 'required');
				$this->form_validation->set_rules('title', 'Title', 'required');
				$this->form_validation->set_rules('detail', 'Content', 'required');
				$this->form_validation->set_rules('secure_code', 'Code', 'required');
			} else {
				$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
				$this->form_validation->set_rules('name', 'Tên', 'required');
				$this->form_validation->set_rules('title', 'Tiêu đề', 'required');
				$this->form_validation->set_rules('detail', 'Nội dung', 'required');
				$this->form_validation->set_rules('secure_code', 'Code', 'required');
			}

			if ($this->form_validation->run() && $this->send_mail()) {
			 	if (empty($_SESSION['lang']))
			 		echo "<script > alert('Thanks for your request with $this->SiteName, We will contact you as soon as possible.'); document.location.href='".base_url()."home' </script>";
			 	else
			 		echo "<script > alert('Cảm ơn bạn đã liên hệ với $this->SiteName  Chúng tôi sẽ liên lạc với bạn trong thơi gian sớm nhất.'); document.location.href='".base_url()."home' </script>";
			 	redirect('home/', 'refresh');
			} else {

			 	$data['missing'] = $this->form_validation->_error_array;
			}
		}
		$data['main']= "contactpage";
		$data['title'] = 'Liên hệ';
		$this->load->view('template/index',$data);
	}

	function send_mail() {

		if($this->input->post("send")) {
		 $a = $this->session->userdata("secure");
		 if( $this->input->post("secure_code") == $a) {
				$title = '';
				$body = '';
				if (empty($_SESSION['lang'])) {
					$title = "$this->SiteName - Contact Letter ";
					$body = "Full Name: ".$this->input->post("name")."<br />From Email:".$this->input->post("email")."<br />Title: ".$this->input->post("title")."<br />Content: ".$this->input->post("detail");
				}
				else {
					$title = "$this->SiteName - Thư Liên hệ ";
					$body = "Họ Tên: ".$this->input->post("name")."<br />Từ Email:".$this->input->post("email")."<br />Tiêu Đề: ".$this->input->post("title")."<br />Nội dung: ".$this->input->post("detail");
				}
				$this->load->model('Mcontact');
				$this->load->model('Memail');
				$mail =$this->Memail->getemail();
				$email=$mail['em_email'];
				//echo $email;
				$now = time();
				$data =array (
						'cont_name' => $this->input->post("name"),
						'cont_phone' => $this->input->post("phone"),
						'cont_email' => $this->input->post("email"),
						'cont_subject' => $this->input->post("title"),
						'cont_content' => $this->input->post("detail"),
						'cont_modify' => $now,
						'cont_status' => '0',
				);
				if(	$this->Mcontact->insert($data) && send_email($email,$title, $body,$this->SiteName) )
					return true;
				return false;
			} else {
				if (empty($_SESSION['lang']))
					$this->form_validation->_error_array["secure_code"] = "Security code wrong.";
				else
					$this->form_validation->_error_array["secure_code"] = "Mã bảo mật không đúng.";
				return false;
			}

		}
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */